<?php
$daten = NULL;
$FirmaID = NULL;
if (!empty ($_SESSION["CMS_User"]["ID"])) {
	if ($_POST) {
		if (!empty ($_POST["FirmaID"])) { //edit
			if (!empty ($_POST["Name"]) && !empty ($_POST["Ort"]) && !empty ($_POST["PLZ"]) && !empty ($_POST["Adresse"]) && !empty ($_POST["Email"]) && !empty ($_POST["Tel"])) {
				$FirmaID = $_POST["FirmaID"];
				$sql = "update firma set Name='" . $_POST["Name"] . "',Ort='" . $_POST["Ort"] . "',PLZ='" . $_POST["PLZ"] . "'
												,Adresse='" . $_POST["Adresse"] . "' 
												,Satavitat='" . $_POST["Satavitat"] . "'
												,LinkMap='" . $_POST["LinkMap"] . "'
												,Website='" . $_POST["Website"] . "'
												,Email='" . $_POST["Email"] . "'
												,Tel='" . $_POST["Tel"] . "'
												,Fax='" . $_POST["Fax"] . "'
												WHERE ID=$FirmaID";
				$_rd = mysql_query($sql, $_db_link);
			}
		} else { //add
			if (!empty ($_POST["Name"]) && !empty ($_POST["Ort"]) && !empty ($_POST["PLZ"]) && !empty ($_POST["Adresse"]) && !empty ($_POST["Email"]) && !empty ($_POST["Tel"])) {
				$sql = "insert INTO firma (UserID,Name,Ort,PLZ,Adresse,Satavitat,LinkMap,Website,Email,Tel,Fax) 
												values (" . $_SESSION["CMS_User"]["ID"] . "
												,'" . $_POST["Name"] . "'
												,'" . $_POST["Ort"] . "'
												,'" . $_POST["PLZ"] . "'
												,'" . $_POST["Adresse"] . "'
												,'" . $_POST["Satavitat"] . "'
												,'" . $_POST["LinkMap"] . "'
												,'" . $_POST["Website"] . "'
												,'" . $_POST["Email"] . "'
												,'" . $_POST["Tel"] . "'
												,'" . $_POST["Fax"] . "')";
				$_rd = mysql_query($sql, $_db_link);
				$FirmaID = mysql_insert_id();
			}
		}
	} else
		if (!empty ($_GET["FirmaID"])) {
			$FirmaID = $_GET["FirmaID"];
		}
	$sql = "select * from firma WHERE ID=$FirmaID";
	$_rd = mysql_query($sql, $_db_link);
	if ($_rd) {
		$daten = mysql_fetch_array($_rd, MYSQL_ASSOC);
	}

}
if ($daten["LinkMap"] != NULL && $daten["LinkMap"] != "") {
	$datamap = $daten["LinkMap"];
	$swdatamap = TRUE;
} else {
	$datamap = "(36.77739269254331, 10.211277133056683)";
	$swdatamap = FALSE;
}
?>
<script type="text/javascript" src="java/formval.js"></script>
<script type="text/javascript">
  function validateOnSubmit(formElem) {
    var elem;
    var errs=0;
    if (!validatePresent(formElem.Name,  'FirmaNameError', true))  	errs = 1; 
	if (!validatePresent(formElem.Ort,  'OrtError', true))         	errs = 1; 
	if (!validatePresent(formElem.PLZ,  'PLZError', true))         	errs = 1;
	if (!validatePresent(formElem.Adresse,  'AdresseError', true)) 	errs = 1;
	if (!validateEmail  (formElem.Email,  'EmailError', true)) 		errs = 1;
	if (!validateTelnr  (formElem.Tel, 'TelError', true)) 			errs = 1;
	if (!validateTelnr  (formElem.Fax, 'FaxError', false)) 			errs = 1;
    if (errs==0){
		formElem.submit();
	}
  };
</script>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true">
</script>
<script type="text/javascript">
var map;
var marker;
function initialize() {
	
	var myLatlng = new google.maps.LatLng<?PHP echo $datamap; ?>;//(36.81258338428396, 10.156345492431683);
	var myOptions = {
		zoom: 7,
		center: myLatlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		disableDefaultUI: true,
		navigationControl:true			
	}
	map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	<?PHP if($swdatamap) { ?>
	marker = new google.maps.Marker({
		position: myLatlng, 
		map: map
		});
	<?PHP } ?>	
	google.maps.event.addListener(map, 'click', function(event) {
			placeMarker(event.latLng);
			});
	function placeMarker(location) {
		if(marker!=null){
			marker.setVisible(false);
		}
		//var clickedLocation = new google.maps.LatLng(location);
		marker = new google.maps.Marker({
		position: location, 
		map: map
		});
		
		map.setCenter(location);
		
		document.getElementById("LinkMap").value =location;	
	}		
	var homeControlDiv = document.createElement('DIV');
	var homeControl = new HomeControl(homeControlDiv, map);
	
	homeControlDiv.index = 1;
	map.controls[google.maps.ControlPosition.TOP_RIGHT].push(homeControlDiv);
}
function showmap(){
	var msgbox=document.getElementById("map_canvas");
	msgbox.style.display = 'block';
	document.getElementById("map_canvas1").style.display = 'block';
	initialize();
	favArea("map_canvas1");
}
function favArea(nameDiv) 
{
	var Element = document.getElementById(nameDiv);
	objh = parseFloat(Element.style.height)/2;
	objw = parseFloat(Element.style.width)/2;
	Element.style.top = Math.floor(Math.round((document.documentElement.offsetHeight/2)+document.documentElement.scrollTop)-objh)+'px';
	Element.style.left = Math.floor(Math.round((document.documentElement.offsetWidth/2)+document.documentElement.scrollLeft)-objw)+'px';
}
function HomeControl(controlDiv, map) {
	controlDiv.style.padding = '5px';
	var controlUI = document.createElement('DIV');
	controlUI.style.cursor = 'pointer';
	controlUI.title = 'Close';
	controlDiv.appendChild(controlUI);
	
	var controlText = document.createElement('DIV');
	controlText.innerHTML = 'Close';
	controlText.setAttribute('class', 'BootenLink');
	
	controlUI.appendChild(controlText);
	
	google.maps.event.addDomListener(controlUI, 'click', function() {
		document.getElementById("map_canvas").style.display = 'none';
		document.getElementById("map_canvas1").style.display = 'none';
		});
}
</script>
<div style="width:100%;background-color:#F1EEF3;margin:0px"> 
<table class="tableEdit">
<?PHP if(isset($FirmaID)){ ?>
	<tr style="display:none">
	<td class="lableEdit"><?PHP  LangText("ID");?>:</td>
	<td class="inputEdit"><input type="text" name="FirmaID" id="FirmaID" READONLY value='<?PHP echo $FirmaID; ?>' /></td>
	<td class="validateEdit"></td>
	</tr>
	<?PHP } ?>
<tr style="display:none">
<td class="lableEdit"><?PHP  LangText("BenutzerID");?>:</td>
<td class="inputEdit"><input type="text" name="UserID" id="UserID" READONLY value='<?PHP echo $_SESSION["CMS_User"]["ID"]; ?>' /></td>
<td class="validateEdit"></td>
</tr>
<tr>
<td class="lableEdit"><?PHP  LangText("FirmaName");?>:*</td>
<td class="inputEdit"><input type="text" name="Name" id="Name" onchange="validatePresent(this, 'FirmaNameError');" value='<?PHP echo $daten["Name"]; ?>' /></td>
<td class="validateEdit">
<input type="label" DISABLED id="FirmaNameError"  value='<?PHP LangText("FirmaNameError");?>' />
</td>
</tr>
<tr>
<td class="lableEdit"><?PHP  LangText("Ort");?>:*</td>
<td class="inputEdit"><input type="text" name="Ort" id="Ort" onchange="validatePresent(this, 'OrtError');" value='<?PHP echo $daten["Ort"]; ?>' /></td>
<td class="validateEdit"><input type="label"  DISABLED  id="OrtError" value='<?PHP LangText("OrtError");?>' /></td>
</tr>
<tr>
<td class="lableEdit"><?PHP  LangText("PLZ");?>:*</td>
<td class="inputEdit"><input type="text" name="PLZ" id="PLZ" onchange="validatePresent(this, 'PLZError');" value='<?PHP echo $daten["PLZ"]; ?>' /></td>
<td class="validateEdit"><input type="label"  DISABLED  id="PLZError" value='<?PHP LangText("PLZError");?>' /></td>
</tr>	
<tr>
<td class="lableEdit"><?PHP  LangText("Adresse");?>:*</td>
<td class="inputEdit"><textarea name="Adresse"  onchange="validatePresent(this, 'AdresseError');" id="Adresse"><?PHP echo $daten["Adresse"]; ?></textarea></td>
<td class="validateEdit"><input type="label"  DISABLED  id="AdresseError" value='<?PHP LangText("AdresseError");?>' /></td>
</tr>
<tr>
<td class="lableEdit"><?PHP  LangText("Satavitat");?>:</td>
<td class="inputEdit"><input type="text" name="Satavitat" id="Satavitat" value='<?PHP echo $daten["Satavitat"]; ?>' /></td>
<td class="validateEdit"><input type="label"  DISABLED  id="SatavitatError" value='<?PHP LangText("SatavitatError");?>' /></td>
</tr>
<tr>
<td class="lableEdit"><?PHP  LangText("LinkMap");?>:</td>
<td class="inputEdit"><input type="text" name="LinkMap" id="LinkMap" READONLY value='<?PHP echo $daten["LinkMap"]; ?>' />
<a class="BootenLink" href="javascript:showmap();"><?PHP LangText("SetMap"); ?></a></td>
<td class="validateEdit"><input type="label"  DISABLED  id="LinkMapError" value='<?PHP LangText("LinkMapError");?>' /></td>
</tr>
<tr>
<td class="lableEdit"><?PHP  LangText("Website");?>:</td>
<td class="inputEdit"><input type="text" name="Website" id="Website" value='<?PHP echo $daten["Website"]; ?>' /></td>
<td class="validateEdit"><input type="label"  DISABLED  id="WebsiteError" value='<?PHP LangText("WebsiteError");?>' /></td>
</tr>
<tr>
<td class="lableEdit"><?PHP  LangText("Email");?>:*</td>
<td class="inputEdit"><input type="text" name="Email" onchange="validateEmail(this, 'EmailError',true);"  id="Email" value='<?PHP echo $daten["Email"]; ?>' /></td>
<td class="validateEdit"><input type="label"  DISABLED  id="EmailError" value='<?PHP LangText("EmailError");?>' /></td>
</tr>
<tr>
<td class="lableEdit"><?PHP  LangText("Tel");?>:*</td>
<td class="inputEdit"><input type="text" name="Tel" id="Tel"  onchange="validateTelnr(this, 'TelError',true);" value='<?PHP echo $daten["Tel"]; ?>' /></td>
<td class="validateEdit"><input type="label"  DISABLED  id="TelError" value='<?PHP LangText("TelError");?>' /></td>
</tr>
<tr>
<td class="lableEdit"><?PHP  LangText("Fax");?>:</td>
<td class="inputEdit"><input type="text" name="Fax" id="Fax" value='<?PHP echo $daten["Fax"]; ?>' /></td>
<td class="validateEdit"><input type="label"  DISABLED  id="FaxError" value='<?PHP LangText("FaxError");?>' /></td>
</tr>
<tr>
<td class="lableEdit"></td>

<td class="inputEdit"><input type="button"  onclick="validateOnSubmit(this.form);" class="BootenLink" value='<?PHP if($FirmaID!=NULL && $FirmaID != 0 ){LangText("Bearbeiten");}else{ LangText("Himzufugen");}?>' />
<input type="button"  onclick="window.location='index.php?tag=<?PHP echo $_GET['tag']; ?>'" class="BootenLink" value='<?PHP LangText("Abbrechen");?>' /></td>
<td class="validateEdit"></td>
</tr>
</table>
</div>
<!--MAP-->
<div id="map_canvas1" style="width: 500px; height: 400px;">
	<div id="map_canvas" style="width: 500px; height: 400px;" ></div>
</div>	